import * as getCourseInfoService from '../../services/getCourseIndex'
import {setcookie} from '../../utils/cookies'
import {getcookie} from '../../utils/cookies'
import {routerRedux} from 'dva/router'
import {message} from "antd"
import {EXPIRE_SECONDS} from '../../utils/constants'
import {hotGroupId} from '../../utils/constants'
import {cetGroupId} from '../../utils/constants'
import {testGroupId} from '../../utils/constants'
import {itGroupId} from '../../utils/constants'
export default {
  namespace: 'index',
  state: {
    groupList: [{courseList:[]},{courseList:[]},{courseList:[]},{courseList:[]}],
    swiperList:[],
    getCourseState:null,
    isLogined:false,//默认用户未登录
  },
  reducers: {
    getCourseIndexSave(state,{payload}){
      if (payload.data.success == true) {
        if (payload.data.data.length >= 2) {
          payload.data.data[1].courseList.splice(2,payload.data.data[1].courseList.length - 1)
        }

        if (payload.data.data.length >= 3) {
          payload.data.data[2].courseList.splice(2,payload.data.data[2].courseList.length - 1)
        }
        if (payload.data.data.length >= 4) {
          payload.data.data[3].courseList.splice(2,payload.data.data[3].courseList.length - 1)
        }
        let groupList = []
         payload.data.data.map((item)=>{
          if (item.groupId == hotGroupId) {
            groupList.push(item)
          } else if (item.groupId == testGroupId) {
            groupList.push(item)
          } else if (item.groupId == itGroupId) {
            groupList.push(item)
          } else if (item.groupId == cetGroupId) {
            groupList.push(item)
          }

         })

        return {
          ...state,groupList:groupList,swiperList:payload.swiperList,
          getCourseState:payload.values.getCourseState
        }
      }
    },
    resetState(state,{payload}){
      return {
        ...state,getCourseState:payload.getCourseState
      }
    },

    saveLoginSituation(state,{payload}){
      // console.log('在reducers里面',payload.isLogined)
      return {
        ...state,
        isLogined: payload.isLogined
      }
    },

  },
  effects: {
    *getCourseIndexInfo({payload:values},{call,put}){
     const {data} = yield call (getCourseInfoService.getCourseIndexInfo,values)
      // console.log("----------data",data)
      let swiperList = []
      if (data.data[0].courseList[0] !=undefined) {
        swiperList.push(data.data[0].courseList[0])
      } else if (data.data[1].courseList[0] !=undefined) {
        swiperList.push(data.data[1].courseList[0])
      } else if (data.data[2].courseList[0] !=undefined) {
        swiperList.push(data.data[2].courseList[0])
      }

      yield put({type:"getCourseIndexSave",payload:{data,swiperList,values}})
      if (data.success == true) {

      }
        },
    *indexToCourse({payload:values},{call,put}){
      setcookie("courseId", values.courseId,EXPIRE_SECONDS);
      // setcookie("unionid", values.unionid,EXPIRE_SECONDS);
      // console.log("------------00",getcookie("courseId"))
      // window.location.replace(`redirect_uri=http%3a%2f%2fv.huanqiushuju.com%2f/courseDetail&courseId=${values.courseId}`);

      yield put(routerRedux.replace({pathname:'/courseDetail',}))

    },
    *indexToMycourse({payload:values},{call,put}){
      // console.log("-------payload",values)
      // window.location.replace(`http%3a%2f%2fv.huanqiushuju.com%2ffcourseDetail&courseId=${payload.courseId}`);
      // window.location.replace(`localhost:8000/courseDetail&courseId=${payload.courseId}`);
      // setcookie("courseId", values.courseId,EXPIRE_SECONDS);
      setcookie("unionid", values.unionid,EXPIRE_SECONDS);


      yield put(routerRedux.replace({pathname:'/myCourse',}))

    },

    //  老师登录
    *teacherLogin({payload:values},{call,put}){
      console.log('laoshi登录时发送的code',values);
      const {data} = yield call (getCourseInfoService.teacherLogin,values)
      if (data.success == true) {//如果登录成功就在网页里存储相关的数据
        console.log("laoshi登录成功以后获取到的数据",data);
        setcookie("headImgUrl",data.data.headImgUrl,EXPIRE_SECONDS);
        setcookie("nickname",data.data.nickname,EXPIRE_SECONDS);
        setcookie("unionid",data.data.unionid,EXPIRE_SECONDS);
        yield put({type:"saveLoginSituation",payload:{isLogined: true}});
        //跳转到管理后台
        yield window.location.replace(`http://adlive.youniwote.com/manage?unionid=${data.data.unionid}&nickname=${data.data.nickname}&headImgUrl=${data.data.headImgUrl}`);
      } else {
        message.error("登录失败，稍后刷新网页重试")
      }
    },
    //  学生登录
    *studentLogin({payload:values},{call,put}){
      // console.log('学生登录时发送的code',values);
      const {data} = yield call (getCourseInfoService.studentLogin,values);
      if (data.success == true) {
        setcookie("headImgUrl",data.data.headImgUrl,EXPIRE_SECONDS);
        setcookie("nickname",data.data.nickname,EXPIRE_SECONDS);
        setcookie("unionid",data.data.unionid,EXPIRE_SECONDS);
        //通过传递Props刷新首页的状态
        yield put({type:"saveLoginSituation",payload:{isLogined: true}});
      } else {
        message.error("登录失败，稍后刷新网页重试")
      }
    },

  },
  subscriptions: {},
};
